
package com.qf.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
	private static String driverClass;
	private static String url;
	private static String user;
	private static String password;
	
	static{
		
		Properties p=new Properties();
		try {
			
			
			
			
			InputStream in = JDBCUtil.class.getResourceAsStream("/configs/db.properties");
			
			p.load(in);
			
			
			driverClass=p.getProperty("driverClass");
			url=p.getProperty("url");
			user=p.getProperty("user");
			password=p.getProperty("password");
			
			Class.forName(driverClass);
			
		} catch (IOException | ClassNotFoundException e) {
			
			
			e.printStackTrace();
				
		}
		
		
		
		
		
		
	}
	
	/**
	 * 获得链接
	* @author: 千锋讲师张增强
	* @createTime: 2017年6月14日 下午4:27:24
	* @history:
	* @return Connection
	 */
	//不去检查未知的错误
	@SuppressWarnings("finally")
	public static Connection getConnection(){
		Connection conn=null;
		try {
			
			//通过DriverManger获得链接
			conn=DriverManager.getConnection(url, user, password);
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			return conn;
		}
	}
	public static  void close(Connection conn,Statement stat){
		if(stat!=null){
			try {
				stat.close();
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
			
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
			
		}
	}
	
	public static  void close(Connection conn,Statement stat,ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
			
		}
		if(stat!=null){
			try {
				stat.close();
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
			
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				
				//TODO
				e.printStackTrace();
					
			}
			
		}
	}
}

	